/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Rent.java
 *
 * Created on Sep 16, 2010, 2:37:05 PM
 */

package COMIC_BOOK_STORE;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author izzah
 */

public class BOOK_RENT extends javax.swing.JFrame {
    Connection con;
    Statement stat;
    String Bcode;
    String Mcode;
    ResultSet rset;
   // String total;

    /** Creates new form Rent */
    public BOOK_RENT() throws SQLException {
         super("RENT BOOK");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
    }

    BOOK_RENT(String a,String b) throws SQLException {
         super("RENT BOOK");
        initComponents();
        connect();
        Bcode = a;
        Mcode = b;
       // total = c;
        memberCodeTextField.setText(b);
        bookCodeTextField.setText(a);
        populateData();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        memberDetailsLabel = new javax.swing.JLabel();
        bookDetailsLabel = new javax.swing.JLabel();
        memberCodeLabel = new javax.swing.JLabel();
        bookCodeLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        memberCodeTextField = new javax.swing.JTextField();
        bookCodeTextField = new javax.swing.JTextField();
        memberCodeLabel2 = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JLabel();
        lastNameLabel = new javax.swing.JLabel();
        icNumberLabel = new javax.swing.JLabel();
        creditBalanceLabel = new javax.swing.JLabel();
        contactNumberLabel = new javax.swing.JLabel();
        displayMemberCode = new javax.swing.JLabel();
        displayFirsttName = new javax.swing.JLabel();
        displayLastName = new javax.swing.JLabel();
        displayIcNumber = new javax.swing.JLabel();
        displayContactNumber = new javax.swing.JLabel();
        displayCreditBalance = new javax.swing.JLabel();
        bookCodeLabel2 = new javax.swing.JLabel();
        bookCostLabel = new javax.swing.JLabel();
        displaybookCode = new javax.swing.JLabel();
        displayBookCost = new javax.swing.JLabel();
        rentOutDateLabel = new javax.swing.JLabel();
        availableLabel = new javax.swing.JLabel();
        displayAvailable = new javax.swing.JLabel();
        rentbutton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        returnDateLabel = new javax.swing.JLabel();
        rentOutDayBox = new javax.swing.JComboBox();
        monthRentBox = new javax.swing.JComboBox();
        yearRentBox = new javax.swing.JComboBox();
        returnDayBox = new javax.swing.JComboBox();
        monthReturnBox = new javax.swing.JComboBox();
        yearReturnBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        memberDetailsLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        memberDetailsLabel.setText("Member Details : ");
        memberDetailsLabel.setBounds(30, 130, 120, 30);
        jLayeredPane1.add(memberDetailsLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookDetailsLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        bookDetailsLabel.setText("Book Details : ");
        bookDetailsLabel.setBounds(390, 130, 110, 30);
        jLayeredPane1.add(bookDetailsLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        memberCodeLabel.setText("Member Code");
        memberCodeLabel.setBounds(60, 30, 130, 30);
        jLayeredPane1.add(memberCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCodeLabel.setText("Book Code");
        bookCodeLabel.setBounds(60, 70, 130, 30);
        jLayeredPane1.add(bookCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 120, 900, -1);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        memberCodeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                memberCodeTextFieldActionPerformed(evt);
            }
        });
        memberCodeTextField.setBounds(220, 30, 140, 30);
        jLayeredPane1.add(memberCodeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);
        bookCodeTextField.setBounds(220, 70, 140, 30);
        jLayeredPane1.add(bookCodeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        memberCodeLabel2.setText("Member Code");
        memberCodeLabel2.setBounds(50, 180, 110, 20);
        jLayeredPane1.add(memberCodeLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        firstNameLabel.setText("First Name");
        firstNameLabel.setBounds(50, 220, 110, 20);
        jLayeredPane1.add(firstNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lastNameLabel.setText("Last Name");
        lastNameLabel.setBounds(50, 260, 110, 20);
        jLayeredPane1.add(lastNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        icNumberLabel.setText("IC Number");
        icNumberLabel.setBounds(50, 300, 100, 20);
        jLayeredPane1.add(icNumberLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        creditBalanceLabel.setText("Credit Balance");
        creditBalanceLabel.setBounds(50, 380, 120, 20);
        jLayeredPane1.add(creditBalanceLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        contactNumberLabel.setText("Contact Number");
        contactNumberLabel.setBounds(50, 340, 120, 20);
        jLayeredPane1.add(contactNumberLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayMemberCode.setText("X");
        displayMemberCode.setBounds(190, 180, 130, 20);
        jLayeredPane1.add(displayMemberCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayFirsttName.setText("X");
        displayFirsttName.setBounds(190, 220, 130, 20);
        jLayeredPane1.add(displayFirsttName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayLastName.setText("X");
        displayLastName.setBounds(190, 260, 120, 20);
        jLayeredPane1.add(displayLastName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayIcNumber.setText("X");
        displayIcNumber.setBounds(190, 300, 140, 20);
        jLayeredPane1.add(displayIcNumber, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayContactNumber.setText("X");
        displayContactNumber.setBounds(190, 340, 140, 20);
        jLayeredPane1.add(displayContactNumber, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayCreditBalance.setText("X");
        displayCreditBalance.setBounds(190, 380, 110, 20);
        jLayeredPane1.add(displayCreditBalance, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCodeLabel2.setText("Book Code");
        bookCodeLabel2.setBounds(400, 180, 110, 20);
        jLayeredPane1.add(bookCodeLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCostLabel.setText("Book Cost");
        bookCostLabel.setBounds(400, 220, 120, 20);
        jLayeredPane1.add(bookCostLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displaybookCode.setText("X");
        displaybookCode.setBounds(530, 180, 110, 20);
        jLayeredPane1.add(displaybookCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayBookCost.setText("X");
        displayBookCost.setBounds(530, 220, 100, 20);
        jLayeredPane1.add(displayBookCost, javax.swing.JLayeredPane.DEFAULT_LAYER);

        rentOutDateLabel.setText("Rent Out Date");
        rentOutDateLabel.setBounds(400, 340, 100, 30);
        jLayeredPane1.add(rentOutDateLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        availableLabel.setText("Availability");
        availableLabel.setBounds(400, 260, 120, 20);
        jLayeredPane1.add(availableLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayAvailable.setText("X");
        displayAvailable.setBounds(530, 260, 150, 20);
        jLayeredPane1.add(displayAvailable, javax.swing.JLayeredPane.DEFAULT_LAYER);

        rentbutton.setText("RENT");
        rentbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rentbuttonActionPerformed(evt);
            }
        });
        rentbutton.setBounds(610, 430, 100, 40);
        jLayeredPane1.add(rentbutton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cancelButton.setText("CANCEL");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        cancelButton.setBounds(720, 430, 100, 40);
        jLayeredPane1.add(cancelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        returnDateLabel.setText("Return Date");
        returnDateLabel.setBounds(400, 380, 90, 30);
        jLayeredPane1.add(returnDateLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        rentOutDayBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Day", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        rentOutDayBox.setBounds(530, 340, 70, 30);
        jLayeredPane1.add(rentOutDayBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        monthRentBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Month", "Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }));
        monthRentBox.setBounds(610, 340, 100, 30);
        jLayeredPane1.add(monthRentBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        yearRentBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Year", "2010", "2011", "2012", "2013", "2014", "2015" }));
        yearRentBox.setBounds(720, 340, 90, 30);
        jLayeredPane1.add(yearRentBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        returnDayBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Day", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        returnDayBox.setBounds(530, 380, 70, 30);
        jLayeredPane1.add(returnDayBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        monthReturnBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Month", "Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }));
        monthReturnBox.setBounds(610, 380, 100, 30);
        jLayeredPane1.add(monthReturnBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        yearReturnBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Year", "2010", "2011", "2012", "2013", "2014", "2015" }));
        yearReturnBox.setBounds(720, 380, 90, 30);
        jLayeredPane1.add(yearReturnBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setBounds(390, 320, 430, 100);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel2.setBounds(30, 160, 330, 260);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel3.setBounds(390, 160, 430, 150);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background5 copy.jpg"))); // NOI18N
        jLabel4.setText("jLabel4");
        jLabel4.setBounds(-40, 0, 940, 500);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    
public static String staffOnDuty = "301";
    
    private void memberCodeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
    }                                                   

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        new MAIN_MENU().setVisible(true);
        this.setVisible(false);
    }                                            

    private void rentbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                           


        String query = null;
       // String query2 = null;
        String a= memberCodeTextField.getText();
        String b = bookCodeTextField.getText();
        String c = displayAvailable.getText();
        String dd = (String) rentOutDayBox.getSelectedItem();
        String mm = (String) monthRentBox.getSelectedItem();
        String yy = (String) yearRentBox.getSelectedItem();
        String outdate = dd+" "+mm+" "+yy;

        String ddd = (String) returnDayBox.getSelectedItem();
        String mmm = (String) monthReturnBox.getSelectedItem();
        String yyy = (String) yearReturnBox.getSelectedItem();
        String returndate = ddd+" "+mmm+" "+yyy;


        if(a.equals("") || b.equals("") || c.equals("NO"))
        {
         JOptionPane.showMessageDialog(null, "Sorry, cannot rent!");
        }

       else
           //(a.equals("MEMBER_CODE") && b.equals("BOOK_CODE"))
        {
             String message = "Are you sure to rent?";
             String title = "RENT BOOK";

            query = "INSERT INTO TRANSACTION VALUES(null, '"+b+"','"+a+"','" +staffOnDuty+"', '" +outdate+"','"+returndate+ "', '"+0+"')";

            int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);
             if(reply == JOptionPane.YES_OPTION)
             {
                try {
                    stat = con.createStatement();
                    stat.executeUpdate(query);

                    JOptionPane.showMessageDialog(null, "Rent Successful");

                    new MAIN_MENU().setVisible(true);
                    this.setVisible(false);

                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_RENT.class.getName()).log(Level.SEVERE, null, ex);
                }


               
             }
        }
        // TODO add your handling code here:
    }                                          

    private void populateData() throws SQLException
    {
        String query = "SELECT * FROM BOOK WHERE BOOK_CODE ='"+Bcode+"'";
        String query1 = "SELECT * FROM MEMBER WHERE MEMBER_CODE='"+Mcode+"'";

        System.out.println("query - " + query);
        System.out.println("query1 - " + query1);

        stat = con.createStatement();
        rset = stat.executeQuery(query);
        Statement stat1;
        ResultSet rset1;

        stat1 = con.createStatement();
        rset1 = stat1.executeQuery(query1);

        while(rset.next() && rset1.next())
        {
          displayMemberCode.setText(rset1.getString("MEMBER_CODE"));
          displayFirsttName.setText(rset1.getString("MEMBER_FNAME"));
          displayLastName.setText(rset1.getString("MEMBER_LNAME"));
          displayIcNumber.setText(rset1.getString("MEMBER_ICNO"));
          displayContactNumber.setText(rset1.getString("MEMBER_CONTACTNO"));
          displayCreditBalance.setText(rset1.getString("MEMBER_CREDIT"));


          displaybookCode.setText(rset.getString("BOOK_CODE"));
          displayBookCost.setText(rset.getString("BOOK_COST"));
          //displayCurrentRenter.setText(rset.getString("BOOK_CURRENT_RENTER"));
          displayAvailable.setText(rset.getString("BOOK_AVAILABLE"));
          //displayRentOutDate.setText(rset.getString("BOOK_RENT_OUT"));
          //displayReturnDate.setText(rset.getString("BOOK_RETURN"));

          
          
        }
    }



    private void connect() throws SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");
        System.out.println("Database Connected");

    }

    /**
    * @param args the command line arguments
    */
   public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new BOOK_RENT().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_RENT.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel availableLabel;
    private javax.swing.JLabel bookCodeLabel;
    private javax.swing.JLabel bookCodeLabel2;
    private javax.swing.JTextField bookCodeTextField;
    private javax.swing.JLabel bookCostLabel;
    private javax.swing.JLabel bookDetailsLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel contactNumberLabel;
    private javax.swing.JLabel creditBalanceLabel;
    private javax.swing.JLabel displayAvailable;
    private javax.swing.JLabel displayBookCost;
    private javax.swing.JLabel displayContactNumber;
    private javax.swing.JLabel displayCreditBalance;
    private javax.swing.JLabel displayFirsttName;
    private javax.swing.JLabel displayIcNumber;
    private javax.swing.JLabel displayLastName;
    private javax.swing.JLabel displayMemberCode;
    private javax.swing.JLabel displaybookCode;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JLabel icNumberLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JLabel memberCodeLabel;
    private javax.swing.JLabel memberCodeLabel2;
    private javax.swing.JTextField memberCodeTextField;
    private javax.swing.JLabel memberDetailsLabel;
    private javax.swing.JComboBox monthRentBox;
    private javax.swing.JComboBox monthReturnBox;
    private javax.swing.JLabel rentOutDateLabel;
    private javax.swing.JComboBox rentOutDayBox;
    private javax.swing.JButton rentbutton;
    private javax.swing.JLabel returnDateLabel;
    private javax.swing.JComboBox returnDayBox;
    private javax.swing.JComboBox yearRentBox;
    private javax.swing.JComboBox yearReturnBox;
    // End of variables declaration                   

}
